/*
 *所有关于Ccd_Report类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Ccd_ReportService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using DGZImp.Core.BaseProvider;
using DGZImp.Core.Extensions.AutofacManager;
using ATkg.BizAppProject.DomainModels;
using System.Linq;
using DGZImp.Core.Utilities;
using System.Linq.Expressions;
using DGZImp.Core.Extensions;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using ATkg.BizAppProject.IRepositories;
using DGZImp.Core.DbSqlSugar;
using DGZImp.Core.Exceptions;

namespace ATkg.BizAppProject.Services
{
    public partial class Ccd_ReportService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ICcd_ReportRepository _repository;//访问数据库

        public AmesSyncService _services { get; set; }

        [ActivatorUtilitiesConstructor]
        public Ccd_ReportService(
            ICcd_ReportRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public async Task<long> CheckSome(string DianduNo)
        {
            var existData = await Db.Set<Ccd_Report>().Where(x => x.DianduNo == DianduNo)
                            .FirstAsync();
            return existData?.Id ?? 0;

        }

        public async Task<long> GetProductEmailInfo(string ProductId)
        {
            var existData = await Db.Set<Ccd_Report>().Where(x => x.DianduNo == ProductId)
                            .FirstAsync();
            return existData?.Id ?? 0;

        }

        //调用接口传入数据时用到了这个新写的函数
        public async Task AddNew(Ccd_Report data)
        {
            var existData = await Db.Set<Ccd_Report>().Where(x=>x.DianduNo == data.DianduNo)
                .FirstAsync();
            if (existData != null)
                throw new AppException("数据重复");
            var obj = await _services.GetProductionTaskByMatLot(data.DianduNo);
            data.ProductId = obj["product_id"][1].ToString().Split(new String[] { "/" }, StringSplitOptions.None)[0];
            data.IncomeWeight = obj["raw_resume_weight"];
            data.Spec = obj["prodcut_spec"];
            data.Grade = obj["grade"][1];
            data.IncomeQty = obj["raw_resume_qty"];
            data.QualifiedRate = (decimal)data.OkQty / Convert.ToDecimal(obj["raw_resume_qty"]); 
            data.AppearanceNgRate = (decimal)data.AppearanceNgQty / Convert.ToDecimal(obj["raw_resume_qty"]);
            data.SizeNgRate = (decimal)data.SizeNgQty / Convert.ToDecimal(obj["raw_resume_qty"]);
            data.GongCanRate = (decimal)data.GongcanQty / Convert.ToDecimal(obj["raw_resume_qty"]);
            data.LossRate = (decimal)data.LossQty / Convert.ToDecimal(obj["raw_resume_qty"]);
            decimal result = (decimal)(data.QualifiedRate + data.AppearanceNgRate + data.SizeNgRate + data.GongCanRate);
            if (result > (decimal)0.8 )
            {
                data.JudgeResult = "数据正常";
            }
            else
            {
                data.JudgeResult = "数据异常";
            }
            await Db.DoInsertAsync(data);
        }

    }
}
